// CPage7.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CharplanDoc.h"
#include "CPage7.h"
#include ".\cpage7.h"


// CPage7-Dialogfeld

IMPLEMENT_DYNAMIC(CPage7, CAPage)
CPage7::CPage7(CWnd* pParent /*=NULL*/)
        : CAPage(CPage7::IDD, pParent)
{
    mn_sort = 0;
}

CPage7::~CPage7()
{
}

void CPage7::DoDataExchange(CDataExchange* pDX)
{
    CAPage::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST2, mwnd_output);
    DDX_Control(pDX, IDC_CHECK1, mwnd_albion);
    DDX_Control(pDX, IDC_CHECK2, mwnd_hibernia);
    DDX_Control(pDX, IDC_CHECK3, mwnd_midgard);
}


BEGIN_MESSAGE_MAP(CPage7, CAPage)
    ON_WM_SIZE()
    ON_NOTIFY(HDN_ITEMCLICK, 0, OnHdnItemclickList2)
    ON_BN_CLICKED(IDC_CHECK3, OnBnClickedCheck3)
    ON_BN_CLICKED(IDC_CHECK1, OnBnClickedCheck1)
    ON_BN_CLICKED(IDC_CHECK2, OnBnClickedCheck2)
END_MESSAGE_MAP()


// CPage7-Meldungshandler

BOOL CPage7::OnInitDialog()
{
    CAPage::OnInitDialog();


    mwnd_output.InsertColumn(0,_T("Realm"));
    mwnd_output.InsertColumn(1,_T("DamType"));
    mwnd_output.InsertColumn(2,_T("Effect"));
    mwnd_output.InsertColumn(3,_T("Class"));
    mwnd_output.InsertColumn(4,_T("Spell Line"));
    mwnd_output.SetExtendedStyle(LVS_EX_FULLROWSELECT);

    mwnd_albion.SetCheck(1);
    mwnd_hibernia.SetCheck(1);
    mwnd_midgard.SetCheck(1);

    FillList();

    return TRUE;
}



void CPage7::OnSize(UINT nType, int cx, int cy)
{
    CAPage::OnSize(nType, cx, cy);

    if (IsWindow(mwnd_output.m_hWnd))
    {
        CRect rect;
        mwnd_output.GetWindowRect(rect);
        ScreenToClient(rect);
        mwnd_output.SetWindowPos(NULL, 0,0,
                                 rect.Width(),cy-2*rect.top,
                                 SWP_NOZORDER | SWP_NOACTIVATE |SWP_NOMOVE);

    }
}

void CPage7::OnHdnItemclickList2(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);

    if (abs(mn_sort) == phdr->iItem+1) mn_sort = -mn_sort;
    else  mn_sort = phdr->iItem+1;

    mwnd_output.SortItems(MyCompareProc,(DWORD_PTR)this);
    for (int t=0;t<mwnd_output.GetItemCount();t++)
        mwnd_output.SetItemData(t,t);


    *pResult = 0;
}


int CALLBACK CPage7::MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
    // lParamSort contains a pointer to the list view control.
    CPage7* ppage = (CPage7*) lParamSort;

    if (ppage->mn_sort==0) return 0;
    int f = abs(ppage->mn_sort)-1;

    CString    strItem1 = ppage->mwnd_output.GetItemText((int)lParam1, f);
    CString    strItem2 = ppage->mwnd_output.GetItemText((int)lParam2, f);

    int res = strItem2.Compare(strItem1);
    if (ppage->mn_sort<0) res = -res;
    return res;
}



void CPage7::FillList()
{

    mwnd_output.DeleteAllItems();

    // Loop REALMS
    CStringArray a_realms;
    gdbinterface.GetListOfRealms(a_realms);
    for (int r=0;r<a_realms.GetCount();r++)
    {
        switch (r)
        {
            case R_ALBION:
                if (!mwnd_albion.GetCheck()) continue;
                break;
            case R_HIBERNIA:
                if (!mwnd_hibernia.GetCheck()) continue;
                break;
            case R_MIDGARD:
                if (!mwnd_midgard.GetCheck()) continue;
                break;
        }
        gdbinterface.SelectRealm(r);

        // Loop CLASSES
        IntArray class_ids;
        gdbinterface.GetListOfClasses(class_ids);

        for (int c=0;c<class_ids.GetCount();c++)
        {
            gdbinterface.SelectClass(class_ids[c]);

            // Loop SPELLLINES
            CStringArray a_lines;
            gdbinterface.GetListOfSpellLines(a_lines);

            for (int l=0;l<a_lines.GetCount();l++)
            {
                gdbinterface.SelectSpellLine(a_lines[l]);

                for (int spec=0;spec<2;spec++)
                {
                    CString   SpellLine_name;
                    CStringArray a_cats;
                    if (spec==0)
                    {
                        SpellLine_name = gdbinterface.GetSpellLineNameBase();
                        gdbinterface.GetListOfSpellCatsBase(a_cats);
                    }
                    else
                    {
                        SpellLine_name = gdbinterface.GetSpellLineNameSpec();
                        gdbinterface.GetListOfSpellCatsSpec(a_cats);
                    }

                    if (SpellLine_name.IsEmpty()) continue;

                    // Loop CATS
                    for (int ca=0;ca<a_cats.GetCount();ca++)
                    {
                        if (spec==0)
                        {
                            gdbinterface.SelectSpellCatBase(a_cats[ca]);
                        }
                        else
                        {
                            gdbinterface.SelectSpellCatSpec(a_cats[ca]);
                        }

                        // Loop SPELLS
                        IntArray  ids;
                        gdbinterface.GetListOfSpells(NULL,NULL,&ids);

                        gdbinterface.SelectSpellID(ids[0]);

                        CStringArray spell_desc;
                        CStringArray spell_values;
                        gdbinterface.GetSpellDescription(spell_desc,spell_values);

                        CString damtype = gdbinterface.GetSpellDamType();
                        if (damtype.IsEmpty()) continue;


                        int item = mwnd_output.InsertItem(0,a_realms[r]);
                        mwnd_output.SetItem(item,1,LVIF_TEXT, gdbinterface.GetSpellDamType(),0,0,0,NULL);
                        mwnd_output.SetItem(item,2,LVIF_TEXT,a_cats[ca],0,0,0,NULL);
                        mwnd_output.SetItem(item,3,LVIF_TEXT, gdbinterface.GetClassName().c_str(),0,0,0,NULL);
                        mwnd_output.SetItem(item,4,LVIF_TEXT,SpellLine_name,0,0,0,NULL);
                    }
                }
            }
        }
    }

    // reset selection
    GetDocument()->Activate();


    // Maik Jurkait [20.12.2004]
    //===========================================
    // sort if needed
    //===========================================
    for (int t=0;t<mwnd_output.GetItemCount();t++)
        mwnd_output.SetItemData(t,t);

    if (mn_sort != 0)
    {
        mwnd_output.SortItems(MyCompareProc,(DWORD_PTR)this);
        for (int t=0;t<mwnd_output.GetItemCount();t++)
            mwnd_output.SetItemData(t,t);
    }


    mwnd_output.SetColumnWidth(0,LVSCW_AUTOSIZE);
    mwnd_output.SetColumnWidth(1,LVSCW_AUTOSIZE);
    mwnd_output.SetColumnWidth(2,LVSCW_AUTOSIZE);
    mwnd_output.SetColumnWidth(3,LVSCW_AUTOSIZE);
    mwnd_output.SetColumnWidth(4,LVSCW_AUTOSIZE);

}


void CPage7::OnBnClickedCheck3()
{
    FillList();
}

void CPage7::OnBnClickedCheck1()
{
    FillList();
}

void CPage7::OnBnClickedCheck2()
{
    FillList();
}
